//
//  GRXShareController.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/14.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXShareController.h"
#import "GRXFastBtn.h"
#import <POP.h>
@interface GRXShareController ()
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@property (strong, nonatomic) IBOutlet UIView *contentView;

/** 标语 */
@property (nonatomic, weak) UIImageView *sloganView;

/**数组,用于存放分享的按钮*/
@property (nonatomic,strong)NSMutableArray<GRXFastBtn *> *buttonArray;

/**存放分享按钮的图片*/
@property (nonatomic,strong)NSArray *imageArray;

/**存放分享按钮的图片*/
@property (nonatomic,strong)NSArray *titleName;

/** 动画时间 */
@property (nonatomic, strong) NSArray *times;

@end

@implementation GRXShareController

/**
 *数据懒加载
 */
- (NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        _buttonArray = [[NSMutableArray alloc] init];
    }
    return _buttonArray;
}

- (NSArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = [[NSArray alloc] init];
        _imageArray = @[@"share_btn_qq",@"share_btn_qqzone",@"share_btn_weixin",@"share_btn_friend",@"share_btn_weibo",@"share_btn_facebook",@"share_btn_twitter",@"share_btn_pinterest"];
    }
    return _imageArray;
}


- (NSArray *)titleName
{
    if (!_titleName) {
        _titleName = [[NSArray alloc] init];
        _titleName = @[@"QQ好友",@"QQ空间",@"微信好友",@"微信朋友圈",@"新浪微博",@"Facebook",@"Twitter",@"Pinterest"];
    }
    return _titleName;
}


- (NSArray *)times
{
    if (!_times) {
        CGFloat interval = 0.1; // 时间间隔
        _times = @[@(5 * interval),
                   @(4 * interval),
                   @(3 * interval),
                   @(2 * interval),
                   @(0 * interval),
                   @(1 * interval),
                   @(6 * interval),
                   @(7 * interval),]; // 标语的动画时间
    }
    return _times;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置取消按钮
    [self setUpButton];
    
    //添加分享按钮
    [self addShareButton];
}
- (IBAction)cancelButtonClick:(id)sender {
    
    [self exitAnimation:^{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self exitAnimation:^{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];}

//添加分享按钮
- (void)addShareButton{
    
    // 一些参数
    NSUInteger count = self.imageArray.count;//按钮的图片个数
    int maxColsCount = 4; // 一行的列数
    NSUInteger rowsCount = (count + maxColsCount - 1) / maxColsCount;//列数
    
    
    // 按钮尺寸
    CGFloat buttonW = (kScreenW - 25) / maxColsCount ;
    CGFloat buttonH = buttonW * 0.85;
    CGFloat buttonStartX = 15;
    CGFloat buttonStartY = (kScreenH - rowsCount * buttonH) * 0.5;
    for (int i = 0; i < count; i++) {
        // 创建、添加
        GRXFastBtn *button = [GRXFastBtn buttonWithType:UIButtonTypeCustom];
        button.width = -1; // 按钮的尺寸为0，还是能看见文字缩成一个点，设置按钮的尺寸为负数，那么就看不见文字了
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.buttonArray addObject:button];
        [self.view addSubview:button];
        
        // 内容
        [button setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        [button setTitle:self.titleName[i] forState:UIControlStateNormal];
        
        // frame
        CGFloat buttonX = (i % maxColsCount) * buttonW + buttonStartX;
        CGFloat buttonY = buttonStartY + (i / maxColsCount) * buttonH * 1.5;
        
        button.y = 667;
        
        // 动画
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY + kScreenW, buttonW, buttonH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        anim.springSpeed = 10;
        anim.springBounciness = 20;
        // CACurrentMediaTime()获得的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[arc4random_uniform(7)] doubleValue];
        [button pop_addAnimation:anim forKey:nil];
    }

}


//退出动画
- (void)exitAnimation:(void(^)())action{
    
    self.view.userInteractionEnabled = NO;
    
    [self.buttonArray enumerateObjectsUsingBlock:^(GRXFastBtn * _Nonnull button, NSUInteger idx, BOOL * _Nonnull stop) {
        
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        anim.toValue = @(button.layer.position.y + kScreenH);
        // CACurrentMediaTime()获得的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[idx] doubleValue];
        [button.layer pop_addAnimation:anim forKey:nil];
        
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        action();
    });
    
    
}


#pragma mark - 点击
- (void)buttonClick:(GRXFastBtn *)button
{
    NSLog(@"点击了%@",button);
}


#pragma mark - 取消按钮设置
- (void)setUpButton{
    self.cancelButton.layer.borderWidth = 0.5;
    self.cancelButton.layer.borderColor = [UIColor whiteColor].CGColor;
}


@end
